package org.platform.processor.knowledge;

import com.mc.mic.core.exception.IllegalParamException;
import org.apache.commons.lang3.StringUtils;
import org.platform.constant.VersionConstant;
import org.platform.dao.mapper.FdKbTestchannelMapper;
import org.platform.dao.mapper.FdKbTestmainMapper;
import org.platform.dao.mapper.FdKbTestparamsMapper;
import org.platform.dao.mapper.FdKbTesttemplateMapper;
import org.platform.mo.csmo.common.*;
import org.platform.mo.db.FdKbTestchannel;
import org.platform.mo.db.FdKbTestmain;
import org.platform.mo.db.FdKbTestparams;
import org.platform.mo.db.FdKbTesttemplate;
import org.platform.tool.helper.ResourceHelper;
import org.platform.tool.util.BeansUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author FengJie
 * @date 2018/4/20
 */
@Service
public class TestProcesor {
    @Autowired
    private FdKbTestmainMapper testMainMapper;
    @Autowired
    private FdKbTestchannelMapper channelMapper;
    @Autowired
    private FdKbTestparamsMapper paramsMapper;
    @Autowired
    private FdKbTesttemplateMapper testTemplateMapper;
    @Autowired
    private ResourceHelper resourceHelper;


    /**
     * 获取对应的test模块
     *
     * @param main
     * @return
     */
    public KnowTestApplyBean getKnowTestByKBMain(KB_Main main) {
        KnowTestApplyBean applyBean = new KnowTestApplyBean();

        Example example = new Example(FdKbTestmain.class);
        Example.Criteria baseCriteria = example.createCriteria()
                .andEqualTo("knowledgeid", main.getKnowledgeid())
                .andEqualTo("station", main.getStation())
                .andEqualTo("version", main.getVersion());


        List<FdKbTestmain> testMian = testMainMapper.selectByExample(example);
        KB_Test test = new KB_Test();
        if (1 == testMian.size()) {
            BeanUtils.copyProperties(testMian.get(0), test);
        } else {
            throw new IllegalParamException("测试主表存在多{knowledgeid}和{station}相同的数据");
        }
        applyBean.setTestMain(test);

        Example channelExample = new Example(FdKbTestchannel.class);
        channelExample.and(baseCriteria);

        List<FdKbTestchannel> testChannelList = channelMapper.selectByExample(channelExample);
        List<KB_TestChannel> channelList = BeansUtils.copyListProperties(testChannelList, KB_TestChannel.class);
        applyBean.setTestChannelList(channelList);

        Example paramsExample = new Example(FdKbTestparams.class);
        paramsExample.and(baseCriteria);

        List<FdKbTestparams> testParamsList = paramsMapper.selectByExample(paramsExample);
        List<KB_TestParam> paramList = BeansUtils.copyListProperties(testParamsList, KB_TestParam.class);
        applyBean.setTestParamList(paramList);

        return applyBean;
    }

    /**
     * 插入对应test模块
     *
     * @param applyBean
     */
    public void addKnowTest(KnowTestApplyBean applyBean) {
        KB_Test testMain = applyBean.getTestMain();
        FdKbTestmain main = new FdKbTestmain();
        BeanUtils.copyProperties(testMain, main);
        testMainMapper.insertSelective(main);

        List<KB_TestChannel> testChannelList = applyBean.getTestChannelList();
        List<FdKbTestchannel> channelList = BeansUtils.copyListProperties(testChannelList, FdKbTestchannel.class);
        channelList.forEach(channel -> channelMapper.insert(channel));

        List<KB_TestParam> testParamList = applyBean.getTestParamList();
        List<FdKbTestparams> testParamsList = BeansUtils.copyListProperties(testParamList, FdKbTestparams.class);
        testParamsList.forEach(test -> paramsMapper.insert(test));
    }


    /**
     * 查询测试模块信息
     *
     * @param knowledgeId 知识id
     * @param station     站点
     * @return 模板
     */
    public FdKbTesttemplate getTestTemplate(Long knowledgeId, String station) {
        Example example = new Example(FdKbTesttemplate.class);
        Example.Criteria criteria = example.createCriteria()
                .andEqualTo("useby", VersionConstant.USE);
        if (knowledgeId != null) {
            criteria.andEqualTo("knowledgeid", knowledgeId);
        }
        if (station != null) {
            criteria.andEqualTo("station", station);
        } else {
            criteria.andEqualTo("station", resourceHelper.getStationName());
        }
        List<FdKbTesttemplate> templateList = testTemplateMapper.selectByExample(example);
        if (!templateList.isEmpty()) {
            return templateList.get(0);
        }
        throw new IllegalParamException("[请求参数]错误,未找到对应的模板信息");
    }


    /**
     * 获取测试参数
     *
     * @param knowledgeId 知识ID
     * @param station     站点
     *                    如果站点为空,默认为当前站点
     * @return json格式
     */
    public FdKbTestparams getTestParamsByKnowledgeID(long knowledgeId, String station) {
        if (StringUtils.isEmpty(station)) {
            station = resourceHelper.getStationName();
        }
        FdKbTestparams testParams = new FdKbTestparams();

        Example paramExample = new Example(FdKbTestparams.class);
        paramExample.createCriteria()
                .andEqualTo("knowledgeid", knowledgeId)
                .andEqualTo("templateid", getTestTemplate(knowledgeId, station).getTemplateid());
        List<FdKbTestparams> paramList = paramsMapper.selectByExample(paramExample);
        if (paramList.size() > 0) {
            testParams = paramList.get(0);
        }
        return testParams;
    }

}
